// pages/teacher/teaclass/teaclass.js
import {parseTime} from '../../../utils/datetime'

import {
  student
} from "../../../api/student";
import {
  scanClass
} from "../../../api/student"

Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },
  options: {
    styleIsolation: 'apply-shared'
  },

  /**
   * 组件的初始数据
   */
  data: {
    classList: [],
    show: false,
    popup: false,
    classid: '',
    classname: '',
    refresh: false,
    page: 1, //页数
    limit: 10, //每页条数
    isLoadings: true, //判断是否有数据

  },

  /**
   * 组件的方法列表
   */
  methods: {
    loadData() {
      let data = {
        page: this.data.page,
        limit: this.data.limit
      }
      student(data).then(res => {
        // console.log(res);
        res.forEach(item=>{
          item.createtime=parseTime(item.createtime,'{y}/{m}/{d}')
        })   
        let classList = this.data.classList

        // console.log(classList);
        classList = classList.concat(res)
        this.setData({
          // classList: [...this.classList,...classList],
          classList: classList,
          refresh: false,
          isLoadings: res.length >= this.data.limit
        })
      })
    },
    refreshmessage() {
      this.setData({
        classList: [],
        page: 1
      })
      this.loadData();

    },
    //上拉加载
    onMoredetail() {
      if (this.data.isLoadings === false) return
      let page = this.data.page
      page++
      this.setData({
        page
      })
      this.loadData();

    },

    onClickShow() {
      this.setData({
        show: true
      });
    },

    onClickHide() {
      this.setData({
        show: false
      });
    },
    noop() {},
    showPopup() {
      this.setData({
        popup: true
      });

    },
    onClose() {
      this.setData({
        popup: false
      });
    },
    onScan() {
      wx.scanCode({
        onlyFromCamera: true,
        scanType: ['qrCode'],
        success: res => {
          let data = {
            id: res.result
          }
          scanClass(data).then(res => {
            console.log(res);
            if (res.code === 200) {
              wx.showToast({
                title: res.msg,
              })
              this.setData({
                show: false,
                popup: false,
                classList: [],
              })

              this.loadData();
            } else if (res.code === 500) {
              wx.showToast({
                title: res.msg,
                icon: 'none'
              })
              this.setData({
                show: false,
                popup: false
              })
            }
          })
        },
        fail: res => {
          console.log("扫码失败");
        }
      })
    },
    //点击开始签到
    signIn(e) {
      console.log(e);
      let item = e.currentTarget.dataset.index;
      if (item.kq_state == 0) return
      wx.navigateTo({
        url: '/pages/student/class/sign',
        success: function (res) {
          res.eventChannel.emit('emitsign', item)
        }
      })

    }

  },
  lifetimes: {
    attached() {
      this.loadData();
      // student().then(res => {
      //   console.log(res.data);
      //   this.setData({
      //     classList: res.data
      //   })
      // })
    }
  }

})